home *** CD-ROM | disk | FTP | other *** search
- /* Copyright, 1990, Regents of the University of Colorado */
- /* This program solves Poisson's equation, by using a row-at-a-time update
- * algorithm. */
-
- #include <stdio.h>
- #include <math.h>
- #include "dino.h"
-
- #define N 8
- #define P 4
-
- environment node[P:id] {
-
- double d[N], s[N];
-
- composite setup() {
-
- int i;
-
- /* Setup d and s so that they can be used by SolveRow */
- d[1] = 2;
- for (i = 2; i < N - 1; i++) {
- s[i] = -1 / d[i - 1];
- d[i] = sqrt (4 - s[i] * s[i]);
- }
-
- }
-
- void solve_row (U, i)
-
- double distributed U[N][N] map BlockRowOverlap;
- int i;
-
- {
- int j;
- double r = 1.0;
- double new[N];
-
- /* Perform the forward solve */
- new [1] = (U[i-1][1] + U[i+1][1] + U[i][0])/d[1];
- for (j=2; j<N-2; j++)
- new [j] = (U[i-1][j] + U[i+1][j] - s[j]*new[j-1])/d[j];
- new [N-2] = (U[i-1][N-2] +U[i+1][N-2] +U[i][N-1] -s[N-2]*new[N-3])/d[N-2];
-
- /* Perform the backward solve */
- new [N-2] = (new[N-2])/d[N-2];
- for (j=N-3; j > 0; j=j-1)
- new [j] = (new[j] - s[j+1]*new[j+1])/d[j];
-
- /* relaxation */
- for (j=1; j<N-1; j++)
- U[i][j] = r*new[j] + (1-r)*U[i][j];
- }
-
- /* Parallel Algorithm For Row-Wise Red Black, with N/P Rows on each
- * processor */
-
- composite solver (U)
-
- double distributed U[N][N] map BlockRowOverlap;
-
- {
- /* Compute the indices of the first and last rows in each block */
- int firstrow = id ? (id * N/P) : 1;
- int lastrow = (id == (P-1)) ? N-2 : ((id+1) * (N/P) - 1);
-
- /* Compute the indices of the first even and first odd rows */
- int firsteven = firstrow%2 ? firstrow + 1: firstrow ;
- int firstodd = firstrow%2 ? firstrow : firstrow + 1;
-
- int i, k; /* Looping variables */
-
- for (k=0; k<10; k++) {
-
- /* Update the odd rows in each block */
- for (i=firstodd; i <= lastrow; i=i+2) {
- if ((i==firstrow) && (i != 1) && (k != 0))
- U[i-1][]#;
- if ((i==lastrow) && (i != N-2) && (k != 0))
- U[i+1][]#;
- solve_row (U,i);
- if (((i==firstrow) && (i != 1)) || ((i==lastrow) && (i != N-2)))
- U[i][]# = U[i][];
- }
-
- /* Update the even rows in each block */
- for (i=firsteven; i <= lastrow; i=i+2) {
- if ((i==firstrow) && (i != 1) && (k != 0))
- U[i-1][]#;
- if ((i==lastrow) && (i != N-2) && (k != 0))
- U[i+1][]#;
- solve_row (U,i);
- if (((i==firstrow) && (i != 1)) || ((i==lastrow) && (i != N-2)))
- U[i][]# = U[i][];
- }
- }
- }
- }
-
- environment host {
-
- main () {
-
- float Uh [N][N];
- int i,j;
-
- /* Initialize the data */
- for (i=0; i<N; i++)
- for (j=0; j<N; j++)
- Uh[i][j] = i*j;
- for (i=1; i<N-1; i++)
- for (j=1; j<N-1; j++)
- Uh[i][j] = Uh[i][j] - .10;
-
- /* Initialize s[] and d[] */
- setup ()#;
-
- /* Compute the results */
- solver (Uh[][])#;
-
- /* Output the results */
- for (i=0; i<N; i++) {
- for (j=0; j<N; j++)
- printf("%6.2f ", Uh[i][j]);
- printf("\\n");
- }
- }
- }
-